﻿/**********************************************
* 作    者：YuBing
* 创建日期：2009年07月13日
* 描    述：Eval/AppraisalEval.aspx 页面 控制Js,批次块
* 修改日期：xxxx年xx月xx日
***********************************************/


/// 新增批次按钮单击事件
function btnAddNewBatches_OnClick() {
	ShowPopForm("divAddAppBatches", "formTitle", APPRAISAL_SUBMIT_NEWAPPBATCHES_TITLE); 	
    var txtBaID = document.getElementById("txtBaID");
    txtBaID.value = "0";
    var objs = getObjects("tdPartObj", "radio");
    objs[0].click();
}

function changePartObj(obj) {
    var divCustomSelectTech = document.getElementById("divCustomSelectTech");
    var txtBaID = document.getElementById("txtBaID");
    var txtPartObj = document.getElementById("txtPartObj");
    txtPartObj.value = obj.value;
    divCustomSelectTech.style.display = "none";
    var frmChild = document.getElementById("frmChild");
    if (obj.value == "0") {
        // 全体教师
        frames["frmChild"].location = "ChildForm/AllTeacherEval.aspx";
    }		    
    if (obj.value == "1") {
        // 教研组        frames["frmChild"].location = "ChildForm/ResearchGroupEval.aspx?BaID="+txtBaID.value;
    }
    if (obj.value == "2") {
        // 年级备课组        frames["frmChild"].location = "ChildForm/GradePreparesGroupEval.aspx?BaID="+txtBaID.value;
    }
    if (obj.value == "3") {
        // 其他
        frames["frmChild"].location = "ChildForm/CustomSelectTeacherEval.aspx?BaID="+txtBaID.value;
        divCustomSelectTech.style.display = "inline";
    }
}

function CloseForm() {
    ClosePopForm("divAddAppBatches");
}


function getCheckbox(doc, parentId) {
    var arr = new Array();
    var parent = doc.getElementById(parentId);
    var cur = parent.firstChild;
    while (cur != null) {
        if ((cur.type == "checkbox" && cur.checked) || (cur.type == "radio" && cur.checked)) {
            if (!arr.container(cur.value)) arr.push(cur.value);
        }
        childCheckbox(cur, arr);
        
        cur = cur.nextSibling;  // 遍历子节点    }
    return arr;
}

function childCheckbox(parent, arr) {
    var cur = parent.firstChild;
    while (cur != null) {
        if ((cur.type == "checkbox" && cur.checked) || (cur.type == "radio" && cur.checked)) {
            if (!arr.container(cur.value)) arr.push(cur.value);
        }
        childCheckboxValues(cur, arr);
        
        cur = cur.nextSibling;  // 遍历子节点    }
}

/// 新增教师
function addTeacher() {
    var doc = document.getElementById("frmAddTeacher").contentWindow.document;
    var obj = doc.getElementById('btnSave');
    obj.click();
}

/// 保存批次
function SaveBatches() {
    var objs = getObjects("tdPartObj", "radio");
    var batchesType;
    
    var txtBaID = document.getElementById("txtBaID");
    var baID = 0;
    if (txtBaID.value != "") baID = parseInt(txtBaID.value);
    
    for (var i=0; i<objs.length; i++) {
        if (objs[i].checked) {
            batchesType = objs[i].value;
            break;
        }
    }
    
    var idList = [];
    
    // 按教研组
    if (batchesType == "1") {
        idList = getCheckedObjs();
        if(idList.length<=0)
        {
            showMassgeBox("系统提示", "请选择教研组!", NOTIC);
            return;
        }
    }
    
    // 按年纪备课组
    if (batchesType == "2") {
        idList = getCheckedObjs();
        if(idList.length<=0)
        {
           showMassgeBox("系统提示", "请选择年级课程!", NOTIC);
          return;
        }
    }
    
    // 按自定义方式
    if (batchesType == "3") {
        idList = getCustomerSelectTeacher();
        if(idList.length<=0)
        {
           showMassgeBox("系统提示", "请选择教师!", NOTIC);
           return;
        }
    }
    
    
    PageMethods.SaveBatches(baID, batchesType, idList, 
        function(succ) {
            if (succ == 3) {
                showMassgeBox(BATCHES_SUBMIT_ALERT_TITLE, BATCHES_SUBMIT_ALERT_CONTENT, ALERT);
                return;
            }        
            ClosePopForm("divAddAppBatches");
            document.getElementById(refeshId).click();
            reloadBatches();
            //refeshObj.click();
            //showMassgeBox(BATCHES_SUBMIT_SUCCESS_TITLE, BATCHES_SUBMIT_SUCCESS_CONTENT, NOTIC, reloadBatches);
        },
        function(err) { 
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }    
    );
}

function getCustomerSelectTeacher() {
    var arr = [];
    var doc = document.getElementById("frmChild").contentWindow.document;
    var list = doc.getElementsByTagName("input");
    for (var i=0; i<list.length; i++) {
        if (list[i].type == "checkbox") {
            if (list[i].checked) {
                arr.push(list[i].value);
            }
        }
    }
    return arr;
}

function reloadBatches() {
    window.location.href = "AppraisalEval.aspx";
}

/// 开始评价批次
function StartBaches(batchesID){
    var batches = {
        "RID": batchesID, 
        "States": 0
    };
    PageMethods.StartBaches(batches, 
        function(succ) {
            document.getElementById("btnRefresh").click();
        },
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );
}

/// 暂停评价批次
function PauseBaches(batchesID){
    var batches = {
        "RID": batchesID, 
        "States": 2
    };
    PageMethods.PauseBaches(batches, 
        function(succ) {
            document.getElementById("btnRefresh").click();
        },
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );
}

/// 恢复评价批次
function ResetBaches(batchesID){
    PageMethods.ResetBaches(batchesID, 
        function(succ) {
            // 成功
            if (succ == 0) {
                document.getElementById("btnRefresh").click();
            }
            // 已有进行中评价
            if (succ == 1) {
                showMassgeBox(APPRAISAL_RESET_BATCHES_CONFIRM_TITLE, APPRAISAL_RESET_BATCHES_CONFIRM_CONTENT);
                return;
            }
        },
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );
}

/// 查看评价批次明细
function ViewBachesDetail(batchesID){
	ShowPopForm("divViewBatchesDetail", "detailTitle", APPRAISAL_SUBMIT_BATCHESDETAIL_TITLE); 
	var frmViewBatchesDetail = document.getElementById("frmViewBatchesDetail");
	frmViewBatchesDetail.src = "ChildForm/TeacherDetailEval.aspx?BaID=" + batchesID;
	var txtViewBaID = document.getElementById("txtViewBaID");
	txtViewBaID.value = batchesID;
}

function selectAllCheckbox(obj) {
    var chks = document.getElementsByName("chk");
    for (var i=0; i<chks.length; i++) {
        chks[i].checked = obj.checked;
    }
}

function selectCheckbox(obj) {
    var chks = document.getElementsByName("chk");
    var flag = true;
    for (var i=0; i<chks.length; i++) {
        if (!chks[i].checked) {
            flag = false;
            break;
        }
    }
    var chkAll = document.getElementById("chkAll");
    chkAll.checked = flag;
}

/// 删除选定批次的结果
function btnDeleteSelectBatchesResults_OnClick() {
    var list = [];
    var chks = document.getElementsByName("chk");
    for (var i=0; i<chks.length; i++) {
        if (chks[i].checked) {
            list.push(parseInt(chks[i].value));
        }
    }
    if(list.length<=0)
    {
        showMassgeBox("系统提示","请选定删除批次!", NOTIC, null);
        return;
    }
    
    
    showConfirmBox("删除确认","是否删除选定批次!",DobtnDeleteSelectBatchesResults,list, null)
}



///执行删除
function DobtnDeleteSelectBatchesResults(list) 
{
     PageMethods.DeleteResultsByTeacherList(list, 
        function(succ) {
            
            document.getElementById(refeshId).click();
            return;
        },
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );

}

/// 清除批次结果
function ClearBachesResults(baID) {
    PageMethods.ClearResults(baID, 
        function(succ) {
            return;
        },
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );
}

function ModifyBaches(baID) {
	ShowPopForm("divAddAppBatches", "formTitle", APPRAISAL_MODIFY_NEWAPPBATCHES_TITLE); 
    var txtBaID = document.getElementById("txtBaID");
    txtBaID.value = baID;
    PageMethods.GetAppBatches(baID, 
        function (appBatches) {
            var objs = getObjects("tdPartObj", "radio");
            for (var i=0; i<objs.length; i++){
                var obj = objs[i];
                if (obj.value == appBatches.BatchesArea.toString()) {
                    obj.checked = true;
                    break;
                }
            }
            changePartObj1(appBatches.BatchesArea, baID);
        }, 
        function(err) {
            showMassgeBox(MSG_ERROR_TITLE,MSG_ERROR_MESSAGE);
            return;
        }
    );
}